namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class OrganTypeBLL
    {
        public int Add(OrganType model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrganType(");
            builder.Append("TypeName)");
            builder.Append(" values (");
            builder.Append("@TypeName)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeName", SqlDbType.NVarChar, 50) };
            cmdParms[0].Value = model.TypeName;
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int TypeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrganType ");
            builder.Append(" where TypeID=@TypeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TypeID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Exists(int TypeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrganType");
            builder.Append(" where TypeID=@TypeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TypeID;
            return DBHelper.Exists(builder.ToString(), cmdParms);
        }

        public List<OrganType> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select TypeID,TypeName ");
            builder.Append(" FROM OrganType ");
            DataSet set = DBHelper.Query(builder.ToString());
            OrganType item = null;
            List<OrganType> list = null;
            if (set.Tables[0].Rows.Count > 0)
            {
                list = new List<OrganType>();
                foreach (DataRow row in set.Tables[0].Rows)
                {
                    item = new OrganType();
                    if ((row["TypeID"] != null) && (row["TypeID"].ToString() != ""))
                    {
                        item.TypeID = int.Parse(row["TypeID"].ToString());
                    }
                    if ((row["TypeName"] != null) && (row["TypeName"].ToString() != ""))
                    {
                        item.TypeName = row["TypeName"].ToString();
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public OrganType GetModel(int TypeID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 TypeID,TypeName from OrganType ");
            builder.Append(" where TypeID=@TypeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = TypeID;
            OrganType type = new OrganType();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                if ((set.Tables[0].Rows[0]["TypeID"] != null) && (set.Tables[0].Rows[0]["TypeID"].ToString() != ""))
                {
                    type.TypeID = int.Parse(set.Tables[0].Rows[0]["TypeID"].ToString());
                }
                if ((set.Tables[0].Rows[0]["TypeName"] != null) && (set.Tables[0].Rows[0]["TypeName"].ToString() != ""))
                {
                    type.TypeName = set.Tables[0].Rows[0]["TypeName"].ToString();
                }
                return type;
            }
            return null;
        }

        public bool Update(OrganType model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrganType set ");
            builder.Append("TypeName=@TypeName");
            builder.Append(" where TypeID=@TypeID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@TypeName", SqlDbType.NVarChar, 50), new SqlParameter("@TypeID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.TypeName;
            cmdParms[1].Value = model.TypeID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

